********************************************************************************
* CREATING SUMMARY STATS   
* File description: Creating table 3, 4, A4.1
********************************************************************************
clear all
set more off

use "use/final_data", clear

merge m:1 LopNr using "raw/SEA/collection", keep(1 3) nogen

replace collection=0 if mi(collection)

gen dispinc_euro=dispinc/9.67

* Table 3

bys LopNr: egen claim_tot_period=sum(bf_tot)

gen group0=cond(claim_tot_period==1,1,0) 
gen group1=cond(claim_tot_period==0,1,0) 
gen group2=cond(claim_tot_period>0,1,0)
gen group3=cond(claim_tot_period>=1 & claim_tot_period<=5,1,0)
gen group4=cond(claim_tot_period>=6 & claim_tot_period<=10,1,0)
gen group5=cond(claim_tot_period>=11 & claim_tot_period<=15,1,0)
gen group6=cond(claim_tot_period>=16 & claim_tot_period<=20,1,0)
gen group7=cond(claim_tot_period>=21,1,0)

bys LopNr: egen mean_dispinc=mean(dispinc_euro)

keep LopNr group* mean_dispinc

duplicates drop 

forv j=1(1)7 {
		

qui estpost sum mean_dispinc if group`j'==1 

est store A`j'
}

esttab A1 A2 A3 A4 A5 A6 A7 using ///
"out\table3.tex", replace 

* Table 4

bys LopNr: egen ever_claim=max(claim_dummy)

eststo clear

qui estpost sum dispinc_euro educ age kids married female unemployed selfemployed early_retirement soc_ben sick_leave, d 

est store A1

qui estpost sum dispinc_euro educ age kids married female unemployed selfemployed early_retirement soc_ben sick_leave if ever_bf==0 & kf_verkstall==0, d 

est store A2


qui estpost sum dispinc_euro educ age kids married female unemployed selfemployed early_retirement soc_ben sick_leave if ever_bf==1, d 

est store A3


qui estpost sum dispinc_euro educ age kids married female unemployed selfemployed early_retirement soc_ben sick_leave if kf_verkstall==1, d 

est store A4


esttab A1 A2 A3 A4 using ///
	"out\table4.tex", replace


*Table A4.1

*Panel a)
use "use/final_data", clear

gen debt_euro=Total_skuld/9.67
gen dispinc_euro=dispinc/9.67

replace dispinc_euro=. if bf_tot==0
replace bf_tot=. if bf_tot==0
replace debt_euro=. if debt_euro==0



collapse (mean) debt_euro (sum) sum_debt=debt_euro (sum) total_no_debt=bf_tot (mean) bf_tot (mean) dispinc_euro, by(female)


gen value_per_claim=sum_debt/total_no_debt

egen mean_all_debt=mean(debt_euro)
egen mean_all_bftot=mean(bf_tot)
egen mean_all_dispinc=mean(dispinc_euro)
egen mean_all_value=mean(value_per_claim)
egen sum_debt_all=sum(sum_debt)
egen sum_no_bf=sum(total_no_debt)

gen share_debt=sum_debt/sum_debt_all
gen share_no_bf=total_no_debt/sum_no_bf

*Panel b)

use "use/final_data" if year==2017, clear

merge 1:1 LopNr using "raw\SEA\collection", keep(3) nogen 

gen debt_euro=(sum_e+sum_a)/9.67
gen dispinc_euro=dispinc/9.67
gen debt_euro_e=sum_e/9.67
gen debt_euro_a=sum_a/9.67


collapse (mean) debt_euro (sum) sum_debt=debt_euro (sum) total_no_debt_a=no_a (sum) total_no_debt_e=no_e (mean) no_e (mean) no_a  (mean) debt_euro_e (mean) debt_euro_a (sum)sum_debt_e=debt_euro_e (sum) sum_debt_a=debt_euro_a (mean) dispinc_euro,  by(female)


egen mean_all_debt=mean(debt_euro)
egen mean_all_debt_a=mean(debt_euro_a)
egen mean_all_debt_e=mean(debt_euro_e)
egen mean_all_no_a=mean(no_a)
egen mean_all_no_e=mean(no_e)
egen mean_all_dispinc=mean(dispinc_euro)
egen sum_debt_all=sum(sum_debt)
egen sum_debt_a_all=sum(sum_debt_a)
egen sum_debt_e_all=sum(sum_debt_e)
egen sum_no_a=sum(total_no_debt_a)
egen sum_no_e=sum(total_no_debt_e)


gen share_debt=sum_debt/sum_debt_all
gen share_debt_a=sum_debt_a/sum_debt_a_all
gen share_no_a=total_no_debt_a/sum_no_a
gen share_no_e=total_no_debt_e/sum_no_e


gen share_no_bf=total_no_debt/sum_no_bf
